package demo;
public class Test {
    //链表头插
    public static Node 头插(Node head,int val){
        Node node=new Node(val);
        node.next=head;
        return node;
    }
    public static void 遍历并打印链表(Node head){
        if (head==null){
            System.out.println("null");
        }
        for (Node cur=head;cur!=null;cur=cur.next){
            System.out.println( cur.val);
        }

    }
    public static Node 头删(Node head){
        return head.next;
    }
    //尾插
    public static Node 尾插(Node head,int val){
        Node node=new Node(5);
        if(head==null){
            return node;
        }

        Node last=head;
        while(last.next!=null){
            last=last.next;//找到最后一个结点
        }
        last.next=node;
        return head;
    }
    public static Node 尾删(Node head){

        //链表有一个结点时
        if(head.next==null){
            return null;
        }
        Node ll=head;
        while(ll.next.next!=null){
            ll=head.next;
        }
        ll.next=null;
        return head;
    }

    public static void main(String[] args) {
        Node head=null;
        head=头插(head,1);
        head=头插(head,2);
        head=头插(head,3);
        head=头插(head,4);
        遍历并打印链表(head);
        System.out.println("------------------");
        head=头删(head);
        head=头删(head);
        遍历并打印链表(head);
        尾插(head,5);
        System.out.println("------------------");
        遍历并打印链表(head);
        System.out.println("------------------");
        尾删(head);
        遍历并打印链表(head);



    }
}
